package net.trustie.model;

import java.util.Date;
import java.util.List;

import net.trustie.utils.DateHandler;
import net.trustie.utils.StringHandler;

import org.apache.commons.codec.digest.DigestUtils;
import org.apache.commons.lang3.StringUtils;

import us.codecraft.webmagic.model.annotation.ExtractBy;
import us.codecraft.webmagic.model.annotation.ExtractBy.Source;
import core.AfterExtractor;
import core.Page;
import core.ValidateExtractor;

@ExtractBy("//*[@id='main']")
public class iteye_blog_Model implements AfterExtractor, ValidateExtractor {
	
	Date dt =new Date();
	private int questionId;

	private String questionUrl;
	private String url_md5;
	private String pageMD5;

	private String history = "0";

	private String extractTime;

	private String tag;
	
	
	@ExtractBy("//*[@id='main']/div[2]/div[1]/h3/a/text()")
	private String questionTitle = "";

	@ExtractBy("//*[@id='blog_content']/allText()")
	private String questionContent = "";

	@ExtractBy( "//*[@id='main']/div[2]/div[1]/ul/li/a/text()")
	private String categories;

	@ExtractBy("//*[@id='main']/div[@class='blog_main']/div[@class='blog_bottom']/ul/li[1]/text()")
	private String postTime = "";
///*[@id="blog_content"]/div[4]/ul/li[1]/
	@ExtractBy("//*[@id='main']/div[2]/div[1]/div/a/allText()")
	private List<String> tags;
	
	private String blog_category;
	@ExtractBy("//*[@id='main']/div[2]/div[1]/ul/li/allText()")
	private List<String> blog_categories;
	
	@ExtractBy(value = "//*[@id='main']/div[@class='blog_main']/div[@class='blog_bottom']/ul/li[3]/a/text()")
	private String answerNum = "0";

	@ExtractBy(value="//*[@id='blog_owner_name']/text()",source=Source.RawHtml)
	private String author;

	@ExtractBy(value="//*[@id='blog_owner_logo']/a/@href",source=Source.RawHtml)
	private String authorUrl;

	@ExtractBy("//*[@id='main']/div[@class='blog_main']/div[@class='blog_bottom']/ul/li[2]/text()")
	private String view_num;

	@ExtractBy("//*[@id='digg_bottom']/div[1]/strong/text()")
	private String voteUp;

	@ExtractBy("//*[@id='digg_bottom']/div[2]/strong/text()")
	private String voteDown;
	
	private String type;
	public void afterProcess(Page page) {
		this.type="blog";
		// 处理questionUrl
		this.questionUrl = page.getPageUrl();

		this.url_md5 = DigestUtils.md5Hex(this.questionUrl);
		
		// 处理tag
		this.tag = StringHandler.combineTags(tags);

		this.blog_category = StringHandler.combineTags( this.blog_categories);
		// 处理questionId
		this.questionId = Integer.parseInt(page.getPageUrl().substring(
				page.getPageUrl().lastIndexOf("/") + 1));

		// 处理answerNum
		if (StringUtils.isNotBlank(this.answerNum)) {
			this.answerNum = StringHandler.matchRightString(this.answerNum,
					"\\d+");
		} else
			this.answerNum = "0";

		// interestNum
		this.view_num = StringHandler.matchRightString(this.view_num,
				"\\d+");

		// 处理抽取时间
		this.extractTime = DateHandler.getExtractTime();

		// 处理pageMD5
		this.pageMD5 = DigestUtils.md5Hex(this.questionTitle + this.answerNum
				+ this.view_num + this.voteUp + this.voteDown);

		// 处理postTime
		this.postTime=DateHandler.formatAllTypeDate(this.postTime,page.getTime());
	    this.postTime=DateHandler.handlerDefaultDate(this.postTime,dt);
	    	
	}

	@Override
	public void validate(Page page) {
		if (StringHandler.isAtLeastOneBlank(this.questionTitle, this.author,
				this.authorUrl)) {
			page.setResultSkip(this, true);
			return;
		}

		if (!StringHandler.canFormatterInteger(this.answerNum, this.view_num, this.voteUp, this.voteDown)) {
			page.setResultSkip(this, true);
		}

		if (!DateHandler.canFormatToDate(this.postTime, this.extractTime))
			page.setResultSkip(this, true);		
	}

	public int getQuestionId() {
		return questionId;
	}

	public String getQuestionUrl() {
		return questionUrl;
	}

	public String getPageMD5() {
		return pageMD5;
	}

	public String getHistory() {
		return history;
	}

	public String getExtractTime() {
		return extractTime;
	}

	public String getTag() {
		return tag;
	}

	

	public String getCategories() {
		return categories;
	}

	public String getPostTime() {
		return postTime;
	}

	public List<String> getTags() {
		return tags;
	}

	public String getAnswerNum() {
		return answerNum;
	}

	public String getAuthor() {
		return author;
	}

	public String getAuthorUrl() {
		return authorUrl;
	}

	public String getView_num() {
		return view_num;
	}

	public String getVoteUp() {
		return voteUp;
	}

	public String getVoteDown() {
		return voteDown;
	}

	public void setQuestionId(int questionId) {
		this.questionId = questionId;
	}

	public void setQuestionUrl(String questionUrl) {
		this.questionUrl = questionUrl;
	}

	public void setPageMD5(String pageMD5) {
		this.pageMD5 = pageMD5;
	}

	public void setHistory(String history) {
		this.history = history;
	}

	public void setExtractTime(String extractTime) {
		this.extractTime = extractTime;
	}

	public void setTag(String tag) {
		this.tag = tag;
	}

	

	public void setCategories(String categories) {
		this.categories = categories;
	}

	public void setPostTime(String postTime) {
		this.postTime = postTime;
	}

	public void setTags(List<String> tags) {
		this.tags = tags;
	}

	public void setAnswerNum(String answerNum) {
		this.answerNum = answerNum;
	}

	public void setAuthor(String author) {
		this.author = author;
	}

	public void setAuthorUrl(String authorUrl) {
		this.authorUrl = authorUrl;
	}

	public void setView_num(String view_num) {
		this.view_num = view_num;
	}

	public void setVoteUp(String voteUp) {
		this.voteUp = voteUp;
	}

	public void setVoteDown(String voteDown) {
		this.voteDown = voteDown;
	}

	public String getQuestionTitle() {
		return questionTitle;
	}

	public String getQuestionContent() {
		return questionContent;
	}

	public void setQuestionTitle(String questionTitle) {
		this.questionTitle = questionTitle;
	}

	public void setQuestionContent(String questionContent) {
		this.questionContent = questionContent;
	}
	
}
